#!/usr/bin/env python
# encoding: utf-8
"""
sampleapp.py

This application generates an RSS xml output based on the files
in a single Nirvanix directory.  You could create a cron job to
generate the RSS at intervals.  When completed it will upload the
RSS file to your nirvanix account and output the full path.

Created by Barry Ruffner on 2008-05-02.
Copyright (c) 2008 Nirvanix. All rights reserved.
"""

import sys
import os
from nirvanix import Nirvanix
from nirvanix import Session
import nirvanixutils

def main():
	args = sys.argv[1:]
	if len(args) < 6:
		print "\r\nUsage: AppKey AppName Username Password NirvanixPath RSSFilePath MakePathPublic[opt] MaxEntries[opt]"
		print "Example: python sampleapp.py XXXX-XXXX-XXXX MyUser MyPassword /mydirectory/ /rssfeed/rss.xml True 30\r\n"
		return
	appKey = args[0]
	appName = args[1]
	username = args[2]
	password = args[3]
	path = args[4]
	outputfile = args[5]
	maxentries = 30
	makepublic = False
	if len(args) > 6:
		makepublic = args[6]
	if len(args) > 7:
		maxentries = args[7]
	
	try:
		session = Session(appKey, username, password)
	except nirvanixutils.NirvanixException, e:
		if e.code == 80001:
			print "Login Failed, Please correct the username and password you provided and try again."
			return
		else:
			print "The following error occured while logging in: " + e.message
	imfs = Nirvanix('IMFS', session);
	options = {'folderPath':path,'pageNumber':'1','pageSize':str(maxentries)}
	try:
		doc = imfs.ListFolder(options,False)
	except nirvanixutils.NirvanixException, e:
		print e
		print e.code
	if makepublic.lower() == 'true':
		sharing = Nirvanix('Sharing', session);
		options = {'sharePath':path}
		try:
			print 'path' + path
			sharing.CreateHostedItem(options,False)
		except nirvanixutils.NirvanixException, e:
			print e
			if e.code == 101:
				print 'You cannot make the root folder public.'
				return
			elif e.code == 70012:
				#already shared we can ignore this error.
				pass
			else:
				print e
				return
			
	print '<?xml version="1.0"?>'
	print '<rss version="0.91">'
	print '	<channel>'
	print '	<title>Sample App RSS Writer</title>'
	print '	<language>en-us</language>'
	files = doc.getElementsByTagName("File")
	for filenode in files:
		filename = filenode.getElementsByTagName("Name")[0].childNodes[0].data
		print '	<item>'
		print '		<title>' + filenode.getElementsByTagName("Name")[0].childNodes[0].data + '</title>'
		options = {"filePath":path+filename}
		doc = imfs.GetDownloadNodes(options, False)
		node = doc.getElementsByTagName("DownloadNode")[0].childNodes[0].data
		# if you have a virtual URL you may need to adjust the path based on your setup.
		if path == "/":
			print '		<link>http://' + node + '/' + appName + '/' + username + '/' + filename + '</link>'
		else:
			print '		<link>http://' + node + '/' + appName + '/' + username + '/' + path + filename + '</link>'
		print '		<description>' + filename + ' was uploaded at ' + filenode.getElementsByTagName("CreatedDate")[0].childNodes[0].data + '</description>'
		print '	</item>'
	print '	</channel>'
	print '</rss>'
	
if __name__ == '__main__':
	main()

